

%macro w2_iris(year);

  libname w2_&year "~/adrec/irs_iw2/&year";

  DATA work.w2_dups_flagged;
    SET w2_&year..iw2ty&year._dups_flagged;
  RUN;

  PROC SORT DATA=work.w2_dups_flagged; BY pik; RUN;
  PROC SORT DATA=work.piks; BY pik; RUN;
  DATA work.pik_match;
    MERGE work.w2_dups_flagged (IN=A) work.piks (IN=B);
    BY pik;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN delete;
    IF _merge=3 THEN flag_inw2=1;
    IF _merge=2 THEN flag_inw2=0;
    DROP _merge;
  RUN;

  PROC EXPORT
    DATA = work.pik_match
    OUTFILE = "projectdir/data/raw_pulls/w2_usiris/iw2ty&year._dups_flagged_usiris_2018q4.dta"
    DBMS = DTA
    REPLACE;
  RUN;


  DATA work.w2_summaryrecs;
    SET w2_&year..iw2ty&year._summaryrecs;
  RUN;

  PROC SORT DATA=work.w2_summaryrecs; BY pik; RUN;
  PROC SORT DATA=work.piks; BY pik; RUN;
  DATA work.pik_match;
    MERGE work.w2_summaryrecs (IN=A) work.piks (IN=B);
    BY pik;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN delete;
    IF _merge=3 THEN flag_inw2=1;
    IF _merge=2 THEN flag_inw2=0;
    DROP _merge;
  RUN;

  PROC EXPORT
    DATA = work.pik_match
    OUTFILE = "projectdir/data/raw_pulls/w2_usiris/iw2ty&year._summaryrecs_usiris_2018q4.dta"
    DBMS = DTA
    REPLACE;
  RUN;

%mend;


libname iris "~/adrec/university_employee_usiris/2018";
proc sql;
  create table work.piks as
  select distinct pik
  from iris.usiris_emnmxwalk_2018q4a_2018q4a
  where pik IS NOT MISSING;
quit;

*%w2_iris(2005);
*%w2_iris(2006);
*%w2_iris(2007);
*%w2_iris(2008);
*%w2_iris(2009);
*%w2_iris(2010);
*%w2_iris(2011);
*%w2_iris(2012);
*%w2_iris(2013);
*%w2_iris(2014);
*%w2_iris(2015);
*%w2_iris(2016);
*%w2_iris(2017);


